﻿using System;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace YaAppoint.Exams
{
    public class ExamItemManager(IRepository<ExamItem, Guid> examItemRepository) : DomainService
    {
        private readonly IRepository<ExamItem, Guid> _examItemRepository = examItemRepository;

        /// <summary>
        /// 新建检查项目
        /// </summary>
        public async Task CreateAsync(ExamItem examItem)
        {
            var sameOuterCodeItem = await _examItemRepository.FirstOrDefaultAsync(x => x.OuterCode.Equals(examItem.OuterCode)&& x.Id!=examItem.Id);
            if (sameOuterCodeItem!=null)
            {
                throw new BusinessException(CustomErrorCodes.OuterCodeAlreadyExist)
                {
                    Data = {
                        {"outerCode",examItem.OuterCode}
                    }
                };
            }

            if (examItem.IsDirectory)
            {
                examItem.Icon = "fa fa-folder";
            }
            else
            {
                examItem.Icon = "fa fa-list";
            }
            
            if (examItem.ParentId != null)
            {
                var parent = await _examItemRepository.FirstOrDefaultAsync(x => x.Id == examItem.ParentId);
                if(parent ==null)
                {
                    throw new BusinessException(CustomErrorCodes.ParentDirNotExist);
                }
                if (!parent.IsDirectory)
                {
                    throw new BusinessException(CustomErrorCodes.ParentIsNotDir);
                }
            }
            await _examItemRepository.InsertAsync(examItem);
        }

        /// <summary>
        /// 保存编辑
        /// </summary>
        public async Task SaveEdit(ExamItem examItem)
        {
            var sameOuterCodeItem = await _examItemRepository.FirstOrDefaultAsync(x => x.OuterCode.Equals(examItem.OuterCode)&& x.InstitutionCode==examItem.InstitutionCode && x.Id!=examItem.Id);
            if (sameOuterCodeItem!=null)
            {
                throw new BusinessException(CustomErrorCodes.OuterCodeAlreadyExist)
                {
                    Data = {
                        {"outerCode",examItem.OuterCode}
                    }
                };
            }
            if (examItem.IsDirectory)
            {
                examItem.Icon = "fa fa-folder";
            }
            else
            {
                examItem.Icon = "fa fa-list";
            }

            if (examItem.ParentId != null)
            {
                var parent = await _examItemRepository.FirstOrDefaultAsync(x => x.Id == examItem.ParentId);
                if (parent == null)
                {
                    throw new BusinessException(CustomErrorCodes.ParentDirNotExist);
                }
                if (!parent.IsDirectory)
                {
                    throw new BusinessException(CustomErrorCodes.ParentIsNotDir);
                }
            }
            await _examItemRepository.UpdateAsync(examItem);
        }

        /// <summary>
        ///  删除检查项目
        /// </summary>
        public async Task DeleteAsync(Guid id)
        { 
            var examItem = await _examItemRepository.FirstOrDefaultAsync(x => x.Id == id);
            if (examItem!=null)
            {
                await DeleterEcursiveAsync(examItem);
            }
        }

        /// <summary>
        /// 递归删除
        /// </summary>
        private async Task DeleterEcursiveAsync(ExamItem examItem)
        {
            var children = await _examItemRepository.GetListAsync(x => x.ParentId == examItem.Id);
            if (children.Count > 0)
            {
                foreach (var child in children)
                {
                  await DeleterEcursiveAsync(child);
                }
            }
            await  _examItemRepository.DeleteAsync(examItem);
        }

    }
}
